package com.pacy.pacybaseproject.activitys;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.LinearLayout;

import com.afollestad.materialdialogs.util.DialogUtils;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.pacy.pacybaseproject.BR;
import com.pacy.pacybaseproject.R;
import com.pacy.pacybaseproject.base.BaseXActivity;
import com.pacy.pacybaseproject.databinding.ActivityCyclingactivityBinding;
import com.pacy.pacybaseproject.fragments.CyclingFragment;
import com.pacy.pacybaseproject.viewmodels.CyclingAcViewModel;

import cn.iwgang.countdownview.CustomCountDownTimer;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;
import me.goldze.mvvmhabit.utils.KLog;

/**
 * 骑行地图
 */
public class CyclingActivity extends BaseXActivity<ActivityCyclingactivityBinding, CyclingAcViewModel> {
    private BottomSheetBehavior<LinearLayout> behavior;
    private BaiduMap mBaiduMap;
    private LocationClient mLocationClient;
    long allTime=2*24*60*60*1000;
    private CustomCountDownTimer timer=new CustomCountDownTimer(allTime,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            binding.chronometer.updateShow(allTime-millisUntilFinished);
        }

        @Override
        public void onFinish() {
        }
    };
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_cyclingactivity;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
//        binding.chronometer.start(3*60*60*1000);
        mBaiduMap=binding.mapView.getMap();
        initMapState();
        timer.start();

        behavior = BottomSheetBehavior.from(binding.meterBottomMenu);
        behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        behavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                } else if (newState == BottomSheetBehavior.STATE_HALF_EXPANDED) {
                } else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });
    }

    public void initMapState(){
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.zoom(16.0f);
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
        //开起交通图
        mBaiduMap.setTrafficEnabled(false);
//        //打开地图定位图层
        mBaiduMap.setMyLocationEnabled(true);

        //定位初始化
        LocationClient.setAgreePrivacy(true);
        try {
            mLocationClient = new LocationClient(CyclingActivity.this);
            //通过LocationClientOption设置LocationClient相关参数
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true); // 打开gps
            option.setCoorType("bd09ll"); // 设置坐标类型
            option.setScanSpan(1000);

//设置locationClientOption
            mLocationClient.setLocOption(option);

//注册LocationListener监听器
            MyLocationListener myLocationListener = new MyLocationListener();
            mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
            mLocationClient.start();
        } catch (Exception e) {
            e.printStackTrace();
            KLog.i(e.toString());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding.mapView.onDestroy();
    }

    @Override
    public void onPause() {
        super.onPause();
        binding.mapView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        binding.mapView.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        binding.mapView.onSaveInstanceState(outState);
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || binding.mapView == null){
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(new LatLng(location.getLatitude(),location.getLongitude())));
        }
    }
}